package gui;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.ImageIcon;


@SuppressWarnings("serial")
public class SystemManagerWindow extends JFrame {

	//Fields
	private JPanel contentPane;
	private JTextField clientSearchTextField;
	private JTextField pictureSearchTextField;
	private DefaultTableModel resultTableClientModel;
	private DefaultTableModel resultTablePictureModel;
	private JTable resultTableClient;
	private JTable resultTablePicture;
	private JScrollPane clientScrollPane;
	private JScrollPane pictureScrollPane;
	private JButton btnAdd;
	private JButton btnRemove;
	private JButton btnUpdate;
	private JLabel lblNewLabel;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane_1;


	
	//Ct'r create the frame 
	public SystemManagerWindow() {
		
		setTitle("System Manager Window");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(250, 30, 800, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		clientSearchTextField = new JTextField();
		clientSearchTextField.setForeground(new Color(128, 0, 0));
		clientSearchTextField.setBounds(194, 72, 134, 28);
		contentPane.add(clientSearchTextField);
		clientSearchTextField.setColumns(10);
		

		final JLabel lblResultSearch = new JLabel("");
		lblResultSearch.setBounds(343, 126, 181, 28);
		contentPane.add(lblResultSearch);
		
		final JComboBox clientOptionComboBox = new JComboBox();
		clientOptionComboBox.setForeground(new Color(128, 0, 0));
		clientOptionComboBox.setModel(new DefaultComboBoxModel(new String[] {"Client ID", "Client User Name", "Client First Name", "Client Last Name"}));
		clientOptionComboBox.setBounds(34, 74, 157, 27);
		contentPane.add(clientOptionComboBox);
		
		pictureSearchTextField = new JTextField();
		pictureSearchTextField.setForeground(new Color(128, 0, 0));
		pictureSearchTextField.setBounds(552, 72, 134, 28);
		contentPane.add(pictureSearchTextField);
		pictureSearchTextField.setColumns(10);
		
		JLabel lblClientSearch = new JLabel("Client Search");
		lblClientSearch.setForeground(new Color(128, 0, 0));
		lblClientSearch.setBounds(34, 44, 92, 16);
		contentPane.add(lblClientSearch);
		
		JLabel lblPictureSearch = new JLabel("Picture Search");
		lblPictureSearch.setForeground(new Color(128, 0, 0));
		lblPictureSearch.setBounds(398, 44, 97, 16);
		contentPane.add(lblPictureSearch);
		
		final JLabel lblRowsCount = new JLabel("");
		lblRowsCount.setBounds(542, 529, 61, 16);
		contentPane.add(lblRowsCount);
		
		final JComboBox pictureOptionComboBox = new JComboBox();
		pictureOptionComboBox.setForeground(new Color(128, 0, 0));
		pictureOptionComboBox.setModel(new DefaultComboBoxModel(new String[] {"Picture ID", "Picture Place", "Picture Time"}));
		pictureOptionComboBox.setBounds(398, 74, 142, 27);
		contentPane.add(pictureOptionComboBox);
		
		resultTableClientModel = new DefaultTableModel(null,new Object[]{"ClientID","UserName","First Name","Last Name","Email","Mobile"});
		resultTablePictureModel = new DefaultTableModel(null,new Object[]{"PictureID","Place","Time"});
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(16, 202, 552, 228);
		contentPane.add(scrollPane);
		
		clientScrollPane = new JScrollPane();
		scrollPane.setViewportView(clientScrollPane);
		resultTableClient = new JTable(resultTableClientModel);
		resultTableClient.setForeground(new Color(128, 0, 0));
		clientScrollPane.setViewportView(resultTableClient);
		clientScrollPane.setVisible(false);
		
		scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(16, 203, 552, 225);
		contentPane.add(scrollPane_1);
		
		pictureScrollPane = new JScrollPane();
		scrollPane_1.setViewportView(pictureScrollPane);
		resultTablePicture = new JTable(resultTablePictureModel);
		resultTablePicture.setForeground(new Color(128, 0, 0));
		pictureScrollPane.setViewportView(resultTablePicture);
		pictureScrollPane.setVisible(false);	
		
		btnAdd = new JButton("Add");
		btnAdd.setForeground(new Color(128, 0, 0));
		btnAdd.setBounds(170, 440, 75, 29);
		contentPane.add(btnAdd);
		btnAdd.setVisible(false);
		
		btnRemove = new JButton("Remove");
		btnRemove.setForeground(new Color(128, 0, 0));
		btnRemove.setBounds(26, 440, 75, 29);
		contentPane.add(btnRemove);
		btnRemove.setVisible(false);
		
		btnUpdate = new JButton("Update");
		btnUpdate.setForeground(new Color(128, 0, 0));
		btnUpdate.setBounds(100, 440, 75, 29);
		contentPane.add(btnUpdate);
		btnUpdate.setVisible(false);

		
		JButton btnClientSearch = new JButton("Search");
		btnClientSearch.setForeground(new Color(128, 0, 0));
		btnClientSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				String clientSearch = clientSearchTextField.getText();
				lblResultSearch.setText("");

				
				clientScrollPane.setVisible(true);
				pictureScrollPane.setVisible(false);
				
				if (clientSearch.isEmpty()){
					JOptionPane.showMessageDialog(null, "Please fill the EMPTY fields and try again");
				}
				
			}
		});
		btnClientSearch.setBounds(204, 112, 117, 29);
		contentPane.add(btnClientSearch);
		
		JButton btnPictureSearch = new JButton("Search");
		btnPictureSearch.setForeground(new Color(128, 0, 0));
		btnPictureSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String pictureSearch = pictureSearchTextField.getText();
				lblResultSearch.setText("");
				
				pictureScrollPane.setVisible(true);
				clientScrollPane.setVisible(false);
				
				if (pictureSearch.isEmpty()){
					JOptionPane.showMessageDialog(null, "please fill the EMPTY fields and try again");
				}
			}
		});
		btnPictureSearch.setBounds(562, 112, 117, 29);
		contentPane.add(btnPictureSearch);
		
		JButton btnLogout = new JButton("LogOut");
		btnLogout.setForeground(new Color(128, 0, 0));
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MainWindow mainWindowFrame = new MainWindow();
				mainWindowFrame.setVisible(true);
				dispose();
			}
		});
		
		btnLogout.setBounds(590, 165, 117, 29);
		contentPane.add(btnLogout);
		
		lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(SystemManagerWindow.class.getResource("/FramePictures/SystemManagerFrame.jpg")));
		lblNewLabel.setBounds(0, 0, 800, 578);
		contentPane.add(lblNewLabel);
	}
	
	public void CleanRowsClientList(){
		
		int counter = resultTableClientModel.getRowCount();
		while(counter!=0){
			resultTableClientModel.removeRow(counter-1);
			counter--;
		}
	}
	public void CleanRowsPictureList(){
		
		int counter = resultTablePictureModel.getRowCount();
		while(counter!=0){
			resultTablePictureModel.removeRow(counter-1);
			counter--;
		}
	}
	public void UpdateClientButtons(int type){
		
		if (type==1)
		{
			btnAdd.setText("Add Client");
			btnRemove.setText("Remove Client");
			btnUpdate.setText("Update Client");
			btnAdd.setVisible(true);
			btnRemove.setVisible(true);
			btnUpdate.setVisible(true);
		}
		else{
			btnAdd.setVisible(false);
			btnRemove.setVisible(false);
			btnUpdate.setVisible(false);
		}
	}
	public void UpdatePictureButtons(int type){
		
		if(type==1){
			btnAdd.setText("Add Picture");
			btnRemove.setText("Remove Picture");
			btnUpdate.setText("Update Picture");
			btnAdd.setVisible(true);
			btnRemove.setVisible(true);
			btnUpdate.setVisible(true);
		}
		else{
			btnAdd.setVisible(false);
			btnRemove.setVisible(false);
			btnUpdate.setVisible(false);
		}
	}
}
